using System;
using System.Runtime.Remoting.Messaging;
using System.Text;

namespace Universe.Remoting.Extensions
{
    static class Info
    {
        public static bool GetMessageInfo(IMessage message, out string typeName, out string methodName)
        {
            IMethodMessage rm = message as IMethodMessage;
            if (rm != null)
            {

                string t = rm.TypeName;
                typeName = t;
                string[] tarr = t.Split(new char[] { ',' });
                if (tarr.Length > 1)
                {
                    typeName = tarr[0];
                }


                string mn = string.IsNullOrEmpty(rm.MethodName) ? String.Empty : rm.MethodName;
                StringBuilder methodBuilder = new StringBuilder(mn);
                Type[] sign = rm.MethodSignature as Type[];
                if (sign != null)
                {
                    methodBuilder.Append("(");
                    for (int i = 0; i < sign.Length; i++)
                    {
                        if (i > 0)
                            methodBuilder.Append(", ");

                        methodBuilder.Append(sign[i].Name);
                    }
                    methodBuilder.Append(")");
                }

                methodName = methodBuilder.ToString();
                return true;
            }
            else
            {
                typeName = null;
                methodName = null;
                return false;
            }

            
        }
    }
}
